package com.duoec.doc.controller;

import com.duoec.doc.dto.BaseResponse;
import com.duoec.doc.dto.request.MockRequest;
import com.duoec.doc.dto.response.MockResponse;
import com.duoec.doc.entity.MockConfig;
import com.duoec.doc.service.MockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xuwenzhen
 * @disable
 * @date 2020/1/31
 */
@RestController
@RequestMapping("/api/mock")
public class MockApiController {
    @Autowired
    private MockService mockService;

    /**
     * 保存某个mock配置
     *
     * @param mockConfig 需要保存的mock配置
     * @return 已经保存的mock配置id
     */
    @PostMapping
    public BaseResponse<String> save(@RequestBody MockConfig mockConfig) {
        return BaseResponse.success(mockService.save(mockConfig));
    }

    /**
     * 删除某个mock配置
     *
     * @param docId  文档ID
     * @param apiId  接口ID
     * @param mockId 需要删除的mock配置id
     * @return 返回状态为200表示成功，否则表示失败
     */
    @DeleteMapping("/{docId}/{apiId}/{mockId}")
    public BaseResponse delete(@PathVariable String docId, @PathVariable String apiId, @PathVariable String mockId) {
        mockService.delete(docId, apiId, mockId);
        return BaseResponse.success();
    }

    /**
     * 获取某个接口的mock配置列表（简要信息）
     *
     * @param docId 文档ID
     * @param apiId 接口ID
     * @return mock配置列表（简要信息）
     */
    @GetMapping("/{docId}/{apiId}/")
    public BaseResponse<List<MockConfig>> getMockConfigList(@PathVariable String docId, @PathVariable String apiId) {
        return BaseResponse.success(mockService.list(docId, apiId));
    }

    /**
     * 获取某个mock配置详情
     *
     * @param docId  文档ID
     * @param apiId  接口ID
     * @param mockId mock配置id
     * @return mock配置详情
     */
    @GetMapping("/{docId}/{apiId}/{mockId}")
    public BaseResponse<MockConfig> getMockConfigDetail(@PathVariable String docId, @PathVariable String apiId, @PathVariable String mockId) {
        return BaseResponse.success(mockService.get(mockId));
    }

    /**
     * 预览Mock输出
     *
     * @param mockId mock配置id
     * @return
     */
    @GetMapping("/test/{mockId}")
    public Object testMock(@PathVariable String mockId) {
        return mockService.mock(mockId);
    }

    /**
     * 保存某个mock配置
     *
     * @param mockConfig 需要保存的mock配置
     * @return 已经保存的mock配置id
     */
    @PostMapping("/test")
    public Object previewMock(@RequestBody MockConfig mockConfig) {
        return mockService.getMockResponse(mockConfig);
    }

    /**
     * 由服务端mock starter发过来的请求
     *
     * @param request 转发请求
     * @return mock响应
     */
    @PostMapping("/rest")
    public MockResponse restMock(@RequestBody MockRequest request) {
        return mockService.restMock(request);
    }
}
